package org.apache.flink.web.controller;

import org.apache.flink.web.domain.DynamicSqlResult;
import org.apache.flink.web.domain.SqlReq;
import org.apache.flink.web.domain.SqlResult;
import org.apache.flink.web.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SqlExecController {

    @Autowired
    private ClientService clientService;

    @GetMapping("/ruok")
    public String ruok() {
        return "OK";
    }

    @PostMapping("/exec")
    public SqlResult exec(@RequestBody SqlReq sql) {
        return clientService.exec(sql.getName(), sql.getSessionId(), sql.getSql(), sql.getLimit());
    }

    @PostMapping("/execDynamicSql")
    public DynamicSqlResult execDynamicSql(@RequestBody SqlReq sql) {
        return new DynamicSqlResult(exec(sql));
    }
}
